import numpy as np
import cv2 as cv
from python_ai.common.xcommon import *

W, H = 400, 300
W2, H2 = (W - 1) // 2, (H - 1) // 2
HALF_RAGE = 140
VALUE = 0.5

f_show = np.zeros((H, W), dtype=np.float32)
# box
# f_show[H2 - HALF_RAGE, W2 - HALF_RAGE: W2 + HALF_RAGE] = VALUE
# f_show[H2 + HALF_RAGE, W2 - HALF_RAGE: W2 + HALF_RAGE] = VALUE
# f_show[H2 - HALF_RAGE: H2 + HALF_RAGE, W2 - HALF_RAGE] = VALUE
# f_show[H2 - HALF_RAGE: H2 + HALF_RAGE, W2 + HALF_RAGE] = VALUE
# point
# f_show[H2 - HALF_RAGE, W2 - HALF_RAGE] = VALUE
# line
f_show[H2 - HALF_RAGE, W2 - HALF_RAGE: W2 + HALF_RAGE] = VALUE
# diagonal
# for delta in range(0, HALF_RAGE):
#     f_show[H2 - delta, W2 - delta] = VALUE

f_show_2 = imzoom_rect(f_show, (2 * W, 2 * H))
cv.imshow('f', f_show_2)

f = np.exp(np.e, f_show)
# f1 = (f ** 2 * 0.33) ** 0.5
# f2 = (f ** 2 * 0.67) ** 0.5
f = np.stack((f, np.zeros_like(f)), axis=2)
print_numpy_ndarray_info(f, 'f')

# inverse fourier
img_complex = cv.idft(f)
eps = 1e-20
print_numpy_ndarray_info(img_complex, 'img_complex')
img_only_magnitude = cv.magnitude(img_complex[:, :, 0], img_complex[:, :, 1])
print_numpy_ndarray_info(img_only_magnitude, 'img_only_magnitude')
img_mag_log = np.log(img_only_magnitude + eps)
print_numpy_ndarray_info(img_mag_log, 'img_mag_log')
img_cv = cv.normalize(img_mag_log, None, 0., 1., cv.NORM_MINMAX)
img_cv *= 255.
img_cv = np.uint8(img_cv)
cv.imshow('img', img_cv)
img_cv = imzoom_rect(img_cv, (2 * W, 2 * H))
cv.imshow('img zoom', img_cv)
cv.imwrite(str(HALF_RAGE) + '.tmp.jpg', img_cv)

cv.waitKey(0)
